package mt.client;

import static org.junit.Assert.*;
import java.util.LinkedList;
import java.util.List;

import mt.Order;
import mt.client.JTableModel.TableType;


import org.junit.Before;
import org.junit.Test;

public class ListUpdaterTest {

	private JTableModel history;
	private String nickName ;
	private LinkedList<Order> sellList = new LinkedList<Order>();
	private LinkedList<Order> purchaseList = new LinkedList<Order>();
	private LinkedList<Order> historyList;
	private JTableModel historySellList;
	private JTableModel historyPurchaseList;
	private ListUpdater l;
	
	@Before
	public void init() {
		historyList = new LinkedList<Order>();
		history = new JTableModel(historyList);
		historySellList = new JTableModel(sellList);
		historyPurchaseList = new JTableModel(purchaseList);
		history.setType(TableType.HISTORY_TABLE);
		l = ListUpdater.getInstance(sellList, purchaseList, history, history, history, "nickName");
	}

	@Test
	public void testGetInstance() {
		assertNotNull(l);
		ListUpdater m = ListUpdater.getInstance(sellList, purchaseList, history,historySellList,historyPurchaseList, "nome");
		assertEquals(l,m);
	}
	
	@Test
	public void testUpdateLists() {
		//15 tests needed
		assertNotNull(l.getPurchaseList());
		assertNotNull(l.getSellList());
		Order o = null;
		l.updateLists(o);
		o = Order.createSellOrder("Name1","stock1",10, 1);
		l.updateLists(o);
		assertTrue(sellList.contains(o));
		assertFalse(historyList.contains(o));
		sellList.remove(o);
		historyList.remove(o);
		Order order = Order.createBuyOrder("nickName", "stock1", 10 , 1);
		l.updateLists(order);
		assertTrue(purchaseList.contains(order));
		assertFalse(historyList.contains(order));
		o = Order.createSellOrder("Name1", "stockDiff", 1, 9);
		l.updateLists(o);
		o = Order.createSellOrder("Name1", "stock1", 1, 9);
		l.updateLists(o);
		o = Order.createSellOrder("Name2", "stock1", 1, 1);
		l.updateLists(o);
		assertEquals(order.getNumberOfUnits(),9);
		o = Order.createSellOrder("Name2", "stock1", 9, 1);
		l.updateLists(o);
		assertFalse(purchaseList.contains(order));
		assertTrue(history.getData().contains(order));		
		o = Order.createBuyOrder("Name1", "stockDiff", 10, 9);
		l.updateLists(o);
		o = Order.createSellOrder("Name1", "stockDiff", 15, 9);
		l.updateLists(o);
		o = Order.createSellOrder("Name3", "stock2", 15, 9);
		l.updateLists(o);
		o = Order.createBuyOrder("Name1", "stockDiff", 10, 9);
		l.updateLists(o);
		o = Order.createBuyOrder("Name1", "stock2", 10, 9);
		l.updateLists(o);
		o = Order.createBuyOrder("Name1", "stock2", 5, 9);
		l.updateLists(o);
		o = Order.createSellOrder("Name4", "stock4", 5, 10);
		l.updateLists(o);
		o = Order.createBuyOrder("Name1", "stock4", 5, 9);
		l.updateLists(o);
	}

}
